package com.micro.school.order.service.impl;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.domain.R;
import com.micro.school.order.entity.PayPrepaymentImport;
import com.micro.school.order.entity.PayPrepaymentImportRecord;
import com.micro.school.order.entity.PayStudentPrepayment;
import com.micro.school.order.mapper.PayPrepaymentImportMapper;
import com.micro.school.order.mapper.PayPrepaymentImportRecordMapper;
import com.micro.school.order.mapper.PayStudentPrepaymentMapper;
import com.micro.school.order.service.PayPrepaymentImportRecordService;
import com.micro.school.order.util.OrderNoGenerator;
import com.micro.school.system.api.RemoteEduService;
import com.micro.school.system.api.domain.vo.SchoolCampusVos;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("PayPrepaymentImportRecordServiceImpl")
public class PayPrepaymentImportRecordServiceImpl implements PayPrepaymentImportRecordService {

    @Resource
    private PayPrepaymentImportRecordMapper payPrepaymentImportRecordMapper;

    @Resource
    private PayStudentPrepaymentMapper payStudentPrepaymentMapper;

    @Autowired
    private RemoteEduService remoteEduService;

    @Override
    public String insertImportRecord(List<PayPrepaymentImportRecord> uploadVos, PayPrepaymentImport payPrepaymentImport) {
        StringBuffer stringBuffer = new StringBuffer();
        int insertOrUpdateBatch = payPrepaymentImportRecordMapper.insertOrUpdateBatch(uploadVos);
        if (insertOrUpdateBatch > 0 && payPrepaymentImport.getImportStatus() == 10){
            // 遍历所有导入记录，转换为预缴费记录
            for (PayPrepaymentImportRecord record : uploadVos) {
                PayStudentPrepayment payStudentPrepayment = new PayStudentPrepayment();
                payStudentPrepayment.setBillNo(OrderNoGenerator.generateOrderNo());
                // 基本信息
                payStudentPrepayment.setStudentName(record.getStudentName());
                payStudentPrepayment.setStudentId(record.getStudentId());
                payStudentPrepayment.setIdNumber(record.getIdNumber());
                // 校区信息
                payStudentPrepayment.setCampusId(record.getCampusId());

                SchoolCampusVos schoolCampusVos = new SchoolCampusVos();
                schoolCampusVos.setId(record.getCampusId());
                schoolCampusVos.setDeleteFlag("0");
                schoolCampusVos.setStatus("1");
                R<List<SchoolCampusVos>> listR1 = remoteEduService.checkCampus(schoolCampusVos);
                if (listR1.getData() != null && listR1.getData().size() > 0) {
                    payStudentPrepayment.setCampusName(listR1.getData().get(0).getCampusName());
                }

                payStudentPrepayment.setCampusName(record.getCampusName());
                // 学院信息
                payStudentPrepayment.setCollegeId(Long.valueOf(record.getCollegeId()));
                payStudentPrepayment.setCollegeName(record.getCollegeName());
                // 模式信息
                payStudentPrepayment.setModelId(Long.valueOf(record.getModelId()));
                payStudentPrepayment.setModelName(record.getModelName());
                // 专业信息
                payStudentPrepayment.setMajorId(Long.valueOf(record.getMajorId()));
                payStudentPrepayment.setMajorName(record.getMajorName());
                // 年级信息
                payStudentPrepayment.setGradeId(Long.valueOf(record.getGradeId()));
                payStudentPrepayment.setGradeName(record.getGradeName());
                // 学年信息
                payStudentPrepayment.setFeeYearId(record.getFeeYearId());
                payStudentPrepayment.setFeeYear(record.getFeeYear());
                // 预缴费项目信息
                payStudentPrepayment.setSubjectId(record.getPrepaymentId());
                payStudentPrepayment.setProjectName(record.getPrepaymentSubject());
                // 金额信息
                payStudentPrepayment.setChargeAmount(record.getChargeAmount());
                // 初始抵扣金额为0
                payStudentPrepayment.setDeductionAmount(BigDecimal.valueOf(0));
                // 支付方式 (默认现金)
                payStudentPrepayment.setPrepaymentMethod(record.getPrepaymentMethod());
                // 状态信息
                // 初始支付状态为未支付
                payStudentPrepayment.setPaymentStatus(10);
                // 初始归集状态为未归集
                payStudentPrepayment.setCollectionStatus(10);
                // 租户信息
                payStudentPrepayment.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));

                // 删除标记
                payStudentPrepayment.setDeleteFlag(record.getDeleteFlag());

                // 创建信息
                payStudentPrepayment.setCreateDept(record.getCreateDept());
                payStudentPrepayment.setCreateBy(record.getCreateBy());
                payStudentPrepayment.setCreateTime(record.getCreateTime());
                payStudentPrepayment.setUpdateBy(record.getUpdateBy());
                payStudentPrepayment.setUpdateTime(record.getUpdateTime());
                payStudentPrepayment.setIsCancel(0);
                payStudentPrepayment.setDeleteFlag("0");
                // 保存预缴费记录
                payStudentPrepaymentMapper.insert(payStudentPrepayment);
            }
        }else{
            stringBuffer.append("数据导入失败！");
        }
        return stringBuffer.toString();
    }

    @Override
    public List<PayPrepaymentImportRecord> selectPayPrepaymentImportRecordList(PayPrepaymentImportRecord payPrepaymentImportRecord) {
        List<PayPrepaymentImportRecord> payPrepaymentImportRecordList = payPrepaymentImportRecordMapper.queryAllByLimit(payPrepaymentImportRecord);
        if (1 == SecurityContextHolder.getUserIdcardState()){
            payPrepaymentImportRecordList.forEach(vo -> {
                if (vo.getIdNumber() != null && vo.getIdNumber().length() > 8) {
                    String maskedIdNumber = vo.getIdNumber().substring(0, vo.getIdNumber().length() - 8) + "********";
                    vo.setIdNumber(maskedIdNumber);
                } else if (vo.getIdNumber() != null) {
                    vo.setIdNumber(vo.getIdNumber().replaceAll(".", "*"));
                }
            });
        }
        return payPrepaymentImportRecordList;
    }

    @Override
    public int remove(Long ids) {
        return payPrepaymentImportRecordMapper.remove(ids, Integer.parseInt(SecurityContextHolder.getUserTenantId()));
    }

    @Override
    public Map<String,Object> listMessage(PayPrepaymentImportRecord payPrepaymentImportRecord) {
        Map<String,Object> map = payPrepaymentImportRecordMapper.listMessage(payPrepaymentImportRecord);
        return map;
    }

}
